package com.quanshu.shop.sort.view;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.exchange.ui.base.BaseFragment;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.goods.view.GoodsListActivity;
import com.quanshu.shop.sort.adapter.SortItemFragmentAdapter;
import com.quanshu.shop.sort.entity.SortTypeEntity;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SortItemFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SortItemFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.fragment_sort_img)
    ImageView fragmentSortImg;
    @BindView(R.id.fragment_sort_rl)
    RecyclerView fragmentSortRl;
    private SortTypeEntity.DataEntity dataEntity;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public SortItemFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SortItemFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SortItemFragment newInstance(String param1, String param2, SortTypeEntity.DataEntity dataEntity1) {
        SortItemFragment fragment = new SortItemFragment();
        fragment.dataEntity = dataEntity1;
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public int setFragmentLayoutID() {
        return R.layout.fragment_sort_item;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        SortItemFragmentAdapter sortItemFragmentAdapter = new SortItemFragmentAdapter(getActivity(), dataEntity.getChildren());
        sortItemFragmentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.item_sort_type1_cl:
                        Log.d("test", "子分类" + dataEntity.getChildren().get(position).getName());
                        Bundle bundle = new Bundle();
                        bundle.putString("goods_list_title", dataEntity.getChildren().get(position).getName());
                        bundle.putString("goods_list_id", dataEntity.getChildren().get(position).getId());
                        BaseTool.goActivity(getActivity(), GoodsListActivity.class,bundle);
                        break;
                }
            }
        });
        fragmentSortRl.setAdapter(sortItemFragmentAdapter);
    }

    @Override
    protected void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        fragmentSortRl.setLayoutManager(gridLayoutManager);
        Glide.with(getActivity()).load(Config.URL_HOST_SHOP + dataEntity.getImg()).into(fragmentSortImg);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
